# coding=utf-8
from phosphorusScripts.bedcore.client import clientApi, viewBinder, viewRequest


class EnumScreen(clientApi.GetScreenNodeCls()):

    def __init__(self, namespace, uiName, param):
        super(EnumScreen, self).__init__(namespace, uiName, param)
        self.namespace = param['namespace']
        """:type: str"""
        self.key = param['key']
        """:type: str"""
        self.valid = param['valid']
        """:type: tuple[str]"""
        self.callback = param['callback']
        """:type: function"""
        self.selected = self.valid.index(param['current'])

    @viewBinder.binding(viewBinder.BF_BindString, '#title_text')
    def getTitleText(self):
        return 'phosphorus.{}.{}.name'.format(self.namespace, self.key)

    @viewBinder.binding(viewBinder.BF_BindInt, '#enum_count')
    def getEnumCount(self):
        return len(self.valid)

    @viewBinder.binding(viewBinder.BF_ToggleChanged, 'toggle.enum_selector')
    def handleEnumSelectorToggle(self, args):
        self.selected = args['index']
        return viewRequest.Refresh

    @viewBinder.binding_collection(viewBinder.BF_BindBool, 'enum_selectors', '#state')
    def getEnumSelectorsState(self, index):
        return index == self.selected

    @viewBinder.binding_collection(viewBinder.BF_BindString, 'enum_selectors', '#text')
    def getEnumSelectorsText(self, index):
        if index >= len(self.valid):
            return ''
        return 'phosphorus.{}.{}.{}.name'.format(self.namespace, self.key, self.valid[index])

    @viewBinder.binding(viewBinder.BF_InteractButtonClick, 'button.ok')
    def handleOkButton(self, args):
        self.callback(self.valid[self.selected])
        self.SetRemove()
        return viewRequest.Refresh

    @viewBinder.binding(viewBinder.BF_InteractButtonClick, 'button.cancel')
    def handleCancelButton(self, args):
        self.SetRemove()
        return viewRequest.Refresh
